package cn.tju.yanbo.commons.anim;

import android.graphics.Point;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.AbsoluteLayout.LayoutParams;
import cn.tju.yanbo.utils.AppConfig;
import cn.tju.yanbo.utils.AppUtils;

public class AppAnimationUtils {

    public static Interpolator DECELERATE_INTERPOLATOR = new DecelerateInterpolator();
    
    public static Animation loadRevolveAnimation(final View view, final Point centroid, final double degree, long duration, final boolean stay, AnimationController controller) {
        final Rect bounds = new Rect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
        
        RevolveAnimation rAnim = new RevolveAnimation(centroid, bounds, degree, controller);
        Log.i("gui", "width="+AppUtils.SCREEN_METRICS.widthPixels+",height="+AppUtils.SCREEN_METRICS.heightPixels+",center.x="+centroid.x+",center..y="+centroid.y);
        
        rAnim.setInterpolator(DECELERATE_INTERPOLATOR);
        //设置动画执行过程用的时间,单位毫秒
        rAnim.setDuration(duration);
        rAnim.setAnimationListener(new AnimationListener() {
            
            @Override
            public void onAnimationStart(Animation animation) {
            }
            
            @Override
            public void onAnimationRepeat(Animation animation) {
                
            }
            
            @Override
            public void onAnimationEnd(Animation animation) {
                if(!stay) return;
                Log.i("revolve", view.getTag()+"-before-animation:bounds.left="+bounds.left+",bounds.top="+bounds.top);
                
                Rect revolvedBounds = AppUtils.revolveRect(bounds, centroid, degree);
                int width = revolvedBounds.right-revolvedBounds.left;
                int height = revolvedBounds.bottom-revolvedBounds.top;
                Log.i("revolve", view.getTag()+"-after-animation:revolve.left="+revolvedBounds.left+",revolve.top="+revolvedBounds.top);
//                view.setLayoutParams(new LayoutParams(width, height, revolvedBounds.left, revolvedBounds.top));
                view.clearAnimation();
//                view.offsetLeftAndRight(revolvedBounds.left-view.getLeft());
//                view.offsetTopAndBottom(revolvedBounds.top-view.getTop());
                Log.i("revolve", view.getTag()+"-after-resetposition:"+"revolve.left="+view.getLeft()+",revolve.top="+view.getTop());
            }
        });
        rAnim.setFillAfter(true);
        
        return rAnim;
    }
    
    
    public static Animation loadTranslateAnimation(final View view, final View targetView, long duration) {
        final int deltaX = targetView.getLeft()-view.getLeft();
        final int deltaY = targetView.getTop()-view.getTop();
        Log.i("gui", "deltaX="+deltaX+",deltaY="+deltaY);
        TranslateAnimation tAnim = new TranslateAnimation(0, deltaX, 0, deltaY);
        tAnim.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }
            @Override
            public void onAnimationRepeat(Animation animation) {
            }
            @Override
            public void onAnimationEnd(Animation animation) {
                view.clearAnimation();
                Log.i("gui", "bf:-left="+view.getLeft()+",top="+view.getTop()+",right="+view.getRight()+",bottom="+view.getBottom());
                view.offsetLeftAndRight(deltaX);
                view.offsetTopAndBottom(deltaY);
                Log.i("gui", "aft:-left="+view.getLeft()+",top="+view.getTop()+",right="+view.getRight()+",bottom="+view.getBottom());
            }
        });
        tAnim.setDuration(AppConfig.ANIMATION_DURATION);
        tAnim.setFillAfter(true);
        return tAnim;
    }
}
